package com.example.bgmachine.bgmachine.sets;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.bgmachine.bgmachine.R;
import com.example.bgmachine.bgmachine.homes.HomePages;

import java.text.DecimalFormat;
import java.util.LinkedList;

public class CheckSetAdapter extends BaseAdapter {


    private Context mContext;
    private LinkedList<CheckSetData> mData = new LinkedList<CheckSetData>();

    public CheckSetAdapter(Context context, LinkedList<CheckSetData> list) {
        this.mContext = context;
        this.mData = list;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // txt_pin txt_content
    @Override
    public View getView(int position, View convertViewtms, ViewGroup parent) {
        CheckSetAdapter.ViewHolder holder = null;
        if (convertViewtms == null) {
            View convertView = View.inflate(mContext, R.layout.frament_set_check_list_item_content, null);
            convertViewtms = convertView;

            holder = new CheckSetAdapter.ViewHolder();
            holder.mCheckSetData = mData.get(position);

            holder.check_item_pin_textview = convertViewtms.findViewById(R.id.check_item_pin_textview);
            holder.check_item_name_textview = convertViewtms.findViewById(R.id.check_item_name_textview);
            holder.frament_check_input_edittext = convertViewtms.findViewById(R.id.frament_check_input_edittext);

            holder.frament_check_list_content_bottom_btn_operation_ok_btn = convertViewtms.findViewById(R.id.frament_check_list_content_bottom_btn_operation_ok_btn);
            holder.frament_check_list_content_bottom_btn_operation_check_btn = convertViewtms.findViewById(R.id.frament_check_list_content_bottom_btn_operation_check_btn);
            holder.frament_check_full_textview = convertViewtms.findViewById(R.id.frament_check_full_textview);
            holder.frament_check_list_content_bottom_btn_operation_ok = convertViewtms.findViewById(R.id.frament_check_list_content_bottom_btn_operation_ok);
            holder.frament_check_list_content_bottom_btn_operation_set = convertViewtms.findViewById(R.id.frament_check_list_content_bottom_btn_operation_set);

            holder.check_item_pin_textview.setText(String.valueOf(holder.mCheckSetData.getMenuid()));
            holder.check_item_name_textview.setText(holder.mCheckSetData.getMenuName());
            holder.frament_check_input_edittext.setText(String.valueOf(holder.mCheckSetData.getSpeedInfor()));
            holder.frament_check_full_textview.setText(holder.mCheckSetData.getGearInfor());
            holder.frament_check_list_content_bottom_btn_operation_ok_btn.setTag(holder);
            holder.frament_check_list_content_bottom_btn_operation_check_btn.setTag(holder);
            holder.frament_check_list_content_bottom_btn_operation_ok.setTag(holder);
            holder.frament_check_list_content_bottom_btn_operation_set.setTag(holder);

            holder.frament_check_list_content_bottom_btn_operation_ok_btn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    ViewHolder vs = (ViewHolder) v.getTag();

                    if (vs == null) {
                        Log.d("aaa", " holder.mOrderContentData.getOrderNumber()==cc=");
                        return;
                    }
                    if (vs.frament_check_input_edittext.getText().length() > 0) {
                        String lena = vs.frament_check_input_edittext.getText().toString();
                        float lenb = Float.parseFloat(lena);
                        lenb += 0.1;
                        DecimalFormat decimalFormat = new DecimalFormat("#0.00");

                        // 调用format()方法进行格式化
                        String formattedNumber = decimalFormat.format(lenb);

                        vs.frament_check_input_edittext.setText(formattedNumber);
                        vs.mCheckSetData.setSpeedInfor(Float.parseFloat(lena));

                    }


                }
            });

            holder.frament_check_list_content_bottom_btn_operation_check_btn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    ViewHolder vs = (ViewHolder) v.getTag();

                    if (vs == null) {
                        Log.d("aaa", " holder.mOrderContentData.getOrderNumber()==cc=");
                        return;
                    }
                    if (vs.frament_check_input_edittext.getText().length() > 0) {
                        String lena = vs.frament_check_input_edittext.getText().toString();
                        float lenb = Float.parseFloat(lena);
                        if (lenb > 0) {
                            lenb -= 0.1;
                            DecimalFormat decimalFormat = new DecimalFormat("#0.00");

                            // 调用format()方法进行格式化
                            String formattedNumber = decimalFormat.format(lenb);

                            vs.frament_check_input_edittext.setText(formattedNumber);
                            vs.mCheckSetData.setSpeedInfor(Float.parseFloat(lena));
                        }

                    }


                }
            });
            holder.frament_check_list_content_bottom_btn_operation_ok.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    ViewHolder vs = (ViewHolder) v.getTag();
                    if (vs.frament_check_input_edittext.getText().length() > 0) {
                        String lena = vs.frament_check_input_edittext.getText().toString();
                        float lenb = Float.parseFloat(lena);
                        if (lenb > 0) {

                            DecimalFormat decimalFormat = new DecimalFormat("#0.00");

                            // 调用format()方法进行格式化
                            String formattedNumber = decimalFormat.format(lenb);

                            vs.frament_check_input_edittext.setText(formattedNumber);
                            vs.mCheckSetData.setSpeedInfor(Float.parseFloat(lena));
                        }

                    }
                    HomePages.mHomePages.updateSpeed(vs.mCheckSetData.getMenuid(),vs.mCheckSetData.getSpeedInfor());
                    HomePages.mHomePages.setParamView();
                }
            });
            holder.frament_check_list_content_bottom_btn_operation_set.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    ViewHolder vs = (ViewHolder) v.getTag();
                    HomePages.mHomePages.upCheckLoadView(vs.frament_check_input_edittext,vs.mCheckSetData );
                }
            });
            convertView.setTag(holder);
        } else {

            holder = (CheckSetAdapter.ViewHolder) convertViewtms.getTag();
            holder.mCheckSetData = mData.get(position);
            holder.check_item_pin_textview = convertViewtms.findViewById(R.id.check_item_pin_textview);
            holder.check_item_name_textview = convertViewtms.findViewById(R.id.check_item_name_textview);
            holder.frament_check_input_edittext = convertViewtms.findViewById(R.id.frament_check_input_edittext);
            holder.frament_check_list_content_bottom_btn_operation_ok_btn = convertViewtms.findViewById(R.id.frament_check_list_content_bottom_btn_operation_ok_btn);
            holder.frament_check_list_content_bottom_btn_operation_check_btn = convertViewtms.findViewById(R.id.frament_check_list_content_bottom_btn_operation_check_btn);
            holder.frament_check_full_textview = convertViewtms.findViewById(R.id.frament_check_full_textview);
            holder.frament_check_list_content_bottom_btn_operation_ok = convertViewtms.findViewById(R.id.frament_check_list_content_bottom_btn_operation_ok);
            holder.frament_check_list_content_bottom_btn_operation_set = convertViewtms.findViewById(R.id.frament_check_list_content_bottom_btn_operation_set);
            holder.check_item_pin_textview.setText(String.valueOf(holder.mCheckSetData.getMenuid()));
            holder.check_item_name_textview.setText(holder.mCheckSetData.getMenuName());
            holder.frament_check_input_edittext.setText(String.valueOf(holder.mCheckSetData.getSpeedInfor()));
            holder.frament_check_full_textview.setText(holder.mCheckSetData.getGearInfor());


            holder.frament_check_list_content_bottom_btn_operation_ok_btn.setTag(holder);
            holder.frament_check_list_content_bottom_btn_operation_check_btn.setTag(holder);
            holder.frament_check_list_content_bottom_btn_operation_ok.setTag(holder);
            holder.frament_check_list_content_bottom_btn_operation_set.setTag(holder);

        }

        return convertViewtms;
    }

    //添加一个元素
    public void add(CheckSetData data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(data);
        notifyDataSetChanged();
    }

    //往特定位置，添加一个元素
    public void add(int position, CheckSetData data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(position, data);
        notifyDataSetChanged();
    }

    public void remove(CheckSetData data) {
        if (mData != null) {
            mData.remove(data);
        }
        notifyDataSetChanged();
    }

    public void remove(int position) {
        if (mData != null) {
            mData.remove(position);
        }
        notifyDataSetChanged();
    }

    public void clear() {
        if (mData != null) {
            mData.clear();
        }
        notifyDataSetChanged();
    }


    private class ViewHolder {

        public TextView check_item_pin_textview = null;
        public TextView check_item_name_textview = null;

        public EditText frament_check_input_edittext = null;
        public Button frament_check_list_content_bottom_btn_operation_ok_btn = null;
        public Button frament_check_list_content_bottom_btn_operation_check_btn = null;
        public TextView frament_check_full_textview = null;
        public Button frament_check_list_content_bottom_btn_operation_ok = null;
        public Button frament_check_list_content_bottom_btn_operation_set = null;


        public CheckSetData mCheckSetData = null;

    }

}
